#coding=utf-8
'''
Created on 2010-3-13

@author: Rock
'''
import MySQLdb
import re,string,urllib,urllib2,util
import sys,os
currpath=sys.path[0]
root_path=currpath[:currpath.find('src')]
sys.path.append(root_path+'src/tools')
from Analyzer import *
import setting

'''
    数据库配置信息
'''


class Route:
    '''
    classdocs
    '''

    def __init__(self,_site='',_title='',_supplier='',_days=0,_type='',_tel='',_price=0,_url01='',_url02=''):

        self.conn=MySQLdb.Connection(setting.server_vacation,setting.username_vacation,setting.password_vacation,setting.database_vacationdb,use_unicode=True,charset='utf8')

        self.site = ''             #站点ID
        self.dest = ''             #目的地
        self.rout_url = ''         #线路url
        self.supplier_url = ''     #供应商url
        self.title = ''            #线路标题
        self.days = 0              #行程天数
        self.type = ''             #行程类型
        self.supplier = ''         #提供商名称
        self.telphone = ''         #服务电话
        self.dates = ''            #发团日期组（以';'分隔）
        self.price = 0             #价格
        self.guide = ''            #导游情况
        self.plane = ''            #行程安排
        self.detail = ''           #简介
        self.outcity = ''          #出发城市
        self.person = ''           #发团人数
        self.go_t = '-'            #出发交通工具
        self.back_t = '-'          #回程交通工具
        self.sights = ''           #线路关键字
        self.hotel = ''            #住宿标准
        self.meal = ''             #餐食标准
        self.traffic01 = ''        #长途交通
        self.traffic02 = ''        #短途交通
        self.tickets = ''          #行程内景点门票
        self.payown = ''           #个人付费说明
        self.img_url = ''          #描述图片url
        self.img_default='no_photo.jpg'
        self.r_type=''

        self.site = _site
        self.title = _title
        self.supplier = _supplier
        self.days = _days
        self.type = _type
        self.telphone = _tel
        self.price = _price
        self.rout_url = _url01
        self.supplier_url = _url02
        self.schedules=[]
        self.expenses=[]
        self.shops=[]


    ''' '''
    def setsights(self,_sights):
        self.sights=_sights;
    ''' '''
    def settitle(self,_title):
        self.title=_title;
    ''' '''
    def setsupplier(self,_supplier):
        self.supplier=_supplier;
    ''' '''
    def setdates(self,_dates):
        self.dates=_dates;
    ''' '''
    def setguide(self,_guide):
        self.guide=_guide;
    ''' '''
    def setdetail(self,_detail):
        self.detail=_detail;
    ''' '''
    def setoutcity(self,_outcity):
        self.outcity=_outcity;
    ''' '''
    def setplane(self,_plane):
        self.plane=_plane;
    ''' '''
    def setperson(self,_person):
        self.person=_person;
    ''' '''
    def setdest(self,_dest):
        self.dest=_dest;
    ''' '''
    def setgo_t(self,_go_t):
        self.go_t = _go_t
    ''' '''
    def setback_t(self,_back_t):
        self.back_t = _back_t
    ''' '''
    def setdates(self,dates):
        self.dates = dates

    def tostr(self):
        return self.site+'|'+self.title+'|'+str(self.days)+'|'+self.rout_url+'|'+self.type+'|'+self.telphone+'|'+self.supplier+'|'+self.img_url+'|'+self.detail
    
    
    '''
                更新线路信息到DB
    '''
    def updRoute(self):

        try:
            curs=self.conn.cursor()
            try: 
                
                u=util.strutil(self.detail)
                self.detail = u.filter_tags()
                
                re_dates = re.compile(r'''[0-9]{4}-[0-9]{2}-[0-9]{2}$''')
                self.dates=self.dates.strip()
                if re_dates.search(self.dates):
                    self.dates=self.dates+';'
                self.dest=re.sub('\s*', '', self.dest)
                if self.days==0:
                    self.days=1
                
                '''
                response={'type':'',
                          'pic':'no_photo.jpg',
                          'go_t':'-',
                          'back_t':'-',
                          '':'',
                          'sights':'',
                          'dests':''}
                '''
                print '*************************************************************'
                plans=[]
                for s in self.schedules:
                    plans.append(s.s_brief)
                analyzer=Analyzer()
                res = analyzer.analyzer(self.outcity, self.rout_url, self.title,self.sights+self.detail, plans)
                self.r_type=res['type']
                self.img_default=res['pic']
                if '-'==self.go_t:
                    self.go_t=res['go_t']
                if '-'==self.back_t:
                    self.back_t=res['back_t']
                self.sights=res['sights']
                self.dest=res['dests']
                print '*************************************************************'
                
                u=util.strutil(self.title)
                self.title=u.filter_tags()
                u=util.strutil(self.detail)
                self.detail=u.filter_tags()
                
                params=(self.site,
                        self.supplier,
                        self.telphone,
                        self.outcity,
                        self.dest,
                        self.rout_url,
                        self.title,
                        self.detail,
                        self.dates,
                        self.days,
                        self.type,
                        self.go_t,
                        self.back_t,
                        self.price,
                        self.guide,
                        self.plane,
                        self.sights,
                        self.hotel,
                        self.meal,
                        self.traffic01,
                        self.traffic02,
                        self.tickets,
                        self.payown,
                        self.img_url,
                        self.img_default,
                        self.r_type
                        )
                curs.callproc('PROC_OYESGO_VACATION_ROUTE_UPD',params)
            except Exception,data:
                print data
            finally:
                curs.close()
        finally:
                pass
 
    '''
                更新线路行程信息到DB
    '''
    def updRoute_Schedules(self):

        try:
            curs=self.conn.cursor()
            try: 
                _places=''
                _briefs=''
                _brfs=''
                _lunchs=''
                _dinners=''
                _traffics=''
                _plcodes=''
                _hotels=''
                for sc in self.schedules:
                    _places=_places+'#_#'+sc.s_place;
                    _briefs=_briefs+'#_#'+sc.s_brief;
                    _brfs=_brfs+'#_#'+sc.s_brf;
                    _lunchs=_lunchs+'#_#'+sc.s_lunch;
                    _dinners=_dinners+'#_#'+sc.s_dinner;
                    _traffics=_traffics+'#_#'+sc.s_traffic;
                    _plcodes=_plcodes+'#_#'+sc.s_plcode;
                    _hotels=_hotels+'#_#'+sc.s_hotel;
                params=(len(self.schedules),
                    self.rout_url,
                    self.outcity,
                   _places[3:],
                   _briefs[3:],
                   _brfs[3:],
                   _lunchs[3:],
                   _dinners[3:],
                   _traffics[3:],
                   _plcodes[3:],
                   _hotels[3:])
                curs.callproc('PROC_OYESGO_VACATIONS_ROUTE_SC_UPD',params)
            except Exception,data:
                print data
            finally:
                curs.close()
        finally:
                pass
    def __del__(self):
        self.conn.close()

    '''
                更新线路购物信息到DB
    '''
    def updRoute_Shops(self):

        try:
            curs=self.conn.cursor()
            try: 
                _names=''
                _types=''
                _times=''
                _places=''
                for sp in self.shops:
                    _names=_names+'#_#'+sp.sp_name;
                    _types=_types+'#_#'+sp.sp_type;
                    _times=_times+'#_#'+sp.sp_time;
                    _places=_places+'#_#'+sp.sp_place;
                params=(len(self.shops),
                    self.rout_url,
                   _names[3:],
                   _types[3:],
                   _times[3:],
                   _places[3:])
                curs.callproc('PROC_OYESGO_VACATIONS_ROUTE_SP_UPD',params)
            except Exception,data:
                print data
            finally:
                curs.close()
        finally:
                pass
    def __del__(self):
        self.conn.close()

    '''
                更新线路自费项目信息到DB
    '''
    def updRoute_Expense(self):

        try:
            curs=self.conn.cursor()
            try: 
                _names=''
                _prices=''
                _places=''
                for e in self.expenses:
                    _names=_names+'#_#'+e.e_name;
                    _prices=_prices+'#_#'+e.e_price;
                    _places=_places+'#_#'+e.e_place;
                params=(len(self.expenses),
                    self.rout_url,
                   _names[3:],
                   _prices[3:],
                   _places[3:])
                curs.callproc('PROC_OYESGO_VACATIONS_ROUTE_E_UPD',params)
            except Exception,data:
                print data
            finally:
                curs.close()
        finally:
                pass
            
    
    def deleteRoute(self):

        try:
            curs=self.conn.cursor()
            try: 
                params=(self.rout_url,)
                curs.callproc('PROC_OYESGO_VACATIONS_ROUTE_DEL',params)
            except Exception,data:
                print data
            finally:
                curs.close()
        finally:
                pass
            
    def __del__(self):
        self.conn.close()
        

'''线路行程'''
class Schedule:
    
    def __init__(self,_num='',_place='',_brief='',_brf='0',_lunch='0',_dinner='0',_traffic='',_plcode='',_hotel=''):
        self.s_num=_num
        self.s_place=_place
        self.s_brief=_brief
        self.s_brf=_brf
        self.s_lunch=_lunch
        self.s_dinner=_dinner
        self.s_traffic=_traffic
        self.s_plcode=_plcode
        self.s_hotel=_hotel
        

'''线路自费项目'''
class Expense:
    
    def __init__(self,_num='',_name='',_price='',_place=''):
        self.e_num=_num
        self.e_name=_name
        self.e_price=_price
        self.e_place=_place


'''线路购物描述'''
class Shop:
    
    def __init__(self,_num='',_name='',_type='',_time='',_place=''):
        self.sp_num=_num
        self.sp_name=_name
        self.sp_type=_type
        self.sp_time=_time
        self.sp_place=_place

if __name__ == '__main__':
    print setting.database_vacationdb